/*
 *  Copyright (C) 2013  Venkat Raghavan G
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 *  
 */
package edu.uic.cs.analyze.commons;

import java.util.ArrayList;
import java.util.List;

import edu.uic.cs.analyze.text.model.Span;

// TODO: Auto-generated Javadoc
/**
 * The Class StringUtils.
 */
public class StringUtils {

	/**
	 * Gets the span.
	 *
	 * @param fullString the full string
	 * @param searchString the search string
	 * @return the span
	 */
	public static List<Span> getSpan(String fullString, String searchString) {
		char[] charArray = fullString.toCharArray();
		List<Span> spanList = new ArrayList<Span>();
		int searchIndex = 0;
		for (int i = 0; i < charArray.length; i++) {
			if (searchIndex == searchString.length()) {
				int beginIndex = i - (searchIndex);
				searchIndex = 0;
				Span span = new Span(beginIndex, i - 1, fullString,
						searchString);
				spanList.add(span);
			}
			if (charArray[i] == searchString.charAt(searchIndex)) {
				searchIndex++;
				continue;
			} else
				searchIndex = 0;

		}
		int i = charArray.length;
		if (searchIndex == searchString.length()) {
			int beginIndex = i - (searchIndex + 1);
			searchIndex = 0;
			Span span = new Span(beginIndex, i - 1, fullString, searchString);
			spanList.add(span);
		}
		return spanList;
	}

	/**
	 * Removes the duplicates.
	 *
	 * @param tokens the tokens
	 * @return the list
	 */
	public static List<String> removeDuplicates(List<String> tokens) {
		List<String> uniqueTokens = new ArrayList<String>();
		for (String s : tokens)
			if (!uniqueTokens.contains(s))
				uniqueTokens.add(s);
		return uniqueTokens;
	}

	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(escapeRegex('{'));
		System.out.println("This is\\a \\a".replace(escapeRegex('\\'),
				"as"));
	}

	/**
	 * Gets the regex for character.
	 *
	 * @param character the character
	 * @return the regex for character
	 */
	public static String escapeRegex(char character) {
		String regexString = character + "";
		if (character == '\"')
			regexString = "\\\"";
		else if (character == '\'')
			regexString = "\\\'";
		else if (character == ' ')
			regexString = "\\s";
		else if (character == '\\')
			regexString = "\\\\";
		else if (character == '	')
			regexString = "\\t";
		else if (character == '\t')
			regexString = "\\t";
		else if (character == '[')
			regexString = "\\\\[";
		else if (character == ']')
			regexString = "\\\\]";
		else if (character == '(')
			regexString = "\\\\(";
		else if (character == ')')
			regexString = "\\\\)";
		else if (character == '{')
			regexString = "\\\\{";
		else if (character == '}')
			regexString = "\\\\}";
		return regexString;
	}

	/**
	 * Escapes the regex characters for a string.
	 *
	 * @param stringExpression the string expression
	 * @return the regex for string
	 */
	public static String escapeRegex(String stringExpression) {
		char[] charArray = stringExpression.toCharArray();
		StringBuffer sb = new StringBuffer();
		for (char c : charArray) {
			sb.append(escapeRegex(c));
		}
		return sb.toString();
	}
	
	

}
